<?php
/**
 * 部门控制器
 * User: xuyiping（xuyiping@liangzihuzhu.com.cn）
 * Date: 2017/11/29
 * Time: 16:46
 */

namespace App\Http\Controllers\Admin\System;

use App\Http\Controllers\Controller;
use App\Model\User\DepartmentModel;
use App\Model\User\UserModel;
use Illuminate\Http\Request;

class DepartmentController extends Controller
{

    /**
     * 保存部门信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {

        $departmentID = $request->input('departmentID');
        $parentID = $request->input('parentID');
        $departmentName = $request->input('departmentName');
        $sort = $request->input('sort');

        if (!ebsig_is_int($parentID, 2)) {
            return response()->json(['code' => 1000001, 'message' => '上级部门不能为空']);
        }

        if (empty($departmentName)) {
            return response()->json(['code' => 1000002, 'message' => '部门名称不能为空']);
        }

        if (!ebsig_is_int($sort)) {
            return response()->json(['code' => 1000003, 'message' => '排序不能为空']);
        }

        //查询上级部门信息
        if ($parentID > 0) {
            $parent_department = DepartmentModel::find($parentID);
            if (!$parent_department) {
                return response()->json(['code' => 1000004, 'message' => '上级部门不存在']);
            }
        }

        if ($departmentID) {
            $department = DepartmentModel::find($departmentID);
            if (!$department) {
                return response()->json(['code' => 1000005, 'message' => '部门信息没有找到']);
            }

            //如果部门名称更改，关联的用户表也要更改
            if ($department->departmentName != $departmentName) {
                UserModel::where('departmentID', $departmentID)->update(['departmentName' => $departmentName]);
            }

        } else {

            $department = new DepartmentModel();
            $department->uuid = makeUuid();
            $department->timeStamp = date('Y-m-d H:i:s');
            $department->createTime = date('Y-m-d H:i:s');
            $department->creator = session('ADMIN_USER_ID');
        }

        $department->departmentName = $departmentName;
        $department->parentID = $parentID;
        $department->sort = $sort;
        $department->save();

        return response()->json(['code' => 200, 'message' => 'ok']);

    }

    /**
     * 查询部门列表及部门select内容
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function search(Request $request) {

        $departmentID = $request->input('id');

        //无限级查询部门数据及生成部门html代码
        $department_obj = new DepartmentModel();

        $department = $department_obj->getDepartments(0);

        $department_html = $department_obj->getDepartmentsHtml($department, 1, $departmentID);
        if ($departmentID > 0) {
            $department_html = '<a href="javascript:;" class="list-group-item" style="padding-left: 15px;" data-id="0">志承软件</a>' . $department_html;
        } else {
            $department_html = '<a href="javascript:;" class="list-group-item active" style="padding-left: 15px;" data-id="0">志承软件</a>' . $department_html;
        }

        return response()->json([
            'code' => 200,
            'message'=> 'ok',
            'data' => [
                'html' => $department_html,
                'select' => $department_obj->getDepartmentsSelectHtml($department)
            ],
        ]);

    }

    /**
     *   ajax返回部门信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function get(Request $request)
    {

        $departmentID = $request->input('departmentID');
        if (!ebsig_is_int($departmentID)) {
            return response()->json(['code' => 1000001, 'message' => '部门信息没有找到']);
        }

        $department = DepartmentModel::select('departmentID', 'departmentName', 'parentID', 'sort')->find($departmentID);
        if (!$department) {
            return response()->json(['code' => 1000001, 'message' => '部门信息没有找到']);
        }

        return response()->json([
            'data' => $department ,
            'message' => 'ok',
            'code' => 200
        ]);

    }

    /**
     * 删除部门
     * @param int $departmentID 表主键 部门ID
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete($departmentID)
    {

        //检查该部门是否有下级部门
        $sub_department = DepartmentModel::where('parentID', $departmentID)->first();
        if ($sub_department) {
            return response()->json(['code' => 1000001, 'message' => '该部门有下级部门，请先处理下级部门']);
        }

        //检查该部门是否有用户
        $user = UserModel::where('departmentID', $departmentID)->first();
        if ($user) {
            return response()->json(['code' => 1000002, 'message' => '该部门有用户，请先处理用户']);
        }

        //删除部门
        $department = DepartmentModel::find($departmentID);
        if ($department) {
            $department->delete();
        }

        return response()->json(['code' => 200, 'message' => 'ok']);

    }
}